
public class P30 {
	public static void main(String[] args) {
		int max = 1;
		int position = 1;
		int power = 5;
		
		while (Math.pow(10, position) < Math.pow(9, power) * position) {
			position++;
		}
				
		max = (int) Math.pow(10, position);
		System.out.println("max:" + max);
		
		int totalSum = 0;
		for (int i = 2; i < max; i++) {
			byte[] b = String.valueOf(i).getBytes();
			int sum = 0;
			for (int j = 0; j < b.length; j++) {
				sum += (int) Math.pow(b[j] - 48, power);
			}
			
			if (sum == i) {
				//System.out.println(i);
				totalSum += i;				
			}
		}
		
		System.out.println(totalSum);
	}
}
